<template>
  <el-card class="mt-10">
    <el-form @submit.prevent>
      <el-form-item label="属性名">
        <el-input
          placeholder="请输入属性名"
          style="width: 200px"
          v-model="AttrFormData.attrName"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          :icon="Plus"
          :disabled="!AttrFormData.attrName"
          @click="addAttrValue"
        >
          添加属性值
        </el-button>
        <el-button @click="cancelAddAttr">取消</el-button>
        <el-table :data="AttrFormData.attrValueList" border class="mt-20">
          <el-table-column
            type="index"
            label="序号"
            width="60"
            align="center"
          />
          <el-table-column label="属性值名称">
            <template v-slot="{ row, $index }">
              <el-input
                v-if="row.isEidt"
                v-model="row.valueName"
                ref="inputRef"
                size="small"
                @blur="clearblur(row, $index)"
                @keyup.enter="quitEdit(row)"
              />
              <div v-else @click="showEdit(row)">{{ row.valueName }}</div>
            </template>
          </el-table-column>
          <el-table-column label="操作">
            <template v-slot="{ row, $index }">
              <el-tooltip
                class="box-item"
                effect="dark"
                content="删除属性"
                placement="bottom-end"
              >
                <span>
                  <el-popconfirm
                    :title="`确定删除${row.valueName}吗?`"
                    @confirm="deleteAttr($index)"
                  >
                    <template #reference>
                      <el-button
                        type="danger"
                        :icon="Delete"
                        size="small"
                      ></el-button>
                    </template>
                  </el-popconfirm>
                </span>
              </el-tooltip>
            </template>
          </el-table-column>
        </el-table>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          :disabled="isbtnDisabled"
          @click="addOrUpdateAttrList"
        >
          保存
        </el-button>
        <el-button @click="cancelAddAttr">取消</el-button>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script lang="ts">
export default {
  name: "AddOrUpdateAttr",
};
</script>

<script lang="ts" setup>
import { ref, nextTick, reactive, computed } from "vue";
import { Plus, Delete } from "@element-plus/icons-vue";
import { useCartgroyStroe } from "@/stores/cartgroy";
import { useAttrStore } from "@/stores/attr";
import type {
  AttrValueItem,
  AttrFormDataItem,
} from "@/api/product/model/attrModel";
import { getAddAttrListApi } from "@/api/product/cartgroyselect";
import { ElMessage } from "element-plus";

const emit = defineEmits(["gobackAttrList"]);

// input实例
const inputRef = ref();

const attrStore = useAttrStore();

const AttrFormData = reactive<AttrFormDataItem>({
  attrName: attrStore.attrName,
  attrValueList: attrStore.attrValueList,
});

// 添加属性值
const addAttrValue = () => {
  AttrFormData.attrValueList.push({
    isEidt: true,
    valueName: "",
  });
  nextTick(() => {
    inputRef.value.focus();
  });
};

// 如果Input每值 清除这一列  有值转文本
const clearblur = (row: AttrValueItem, index: number) => {
  if (!row.valueName) {
    AttrFormData.attrValueList.splice(index, 1);
    return;
  }
  row.isEidt = false;
};

//
const quitEdit = (row: AttrValueItem) => {
  row.isEidt = false;
};

// 点击div返回编辑模式
const showEdit = (row: AttrValueItem) => {
  row.isEidt = true;
  nextTick(() => {
    inputRef.value.focus();
  });
};

// 点击取消按钮
const cancelAddAttr = () => {
  AttrFormData.attrName = "";
  AttrFormData.attrValueList = [];
  emit("gobackAttrList", true);
};

// 删除点击的属性值那一列
const deleteAttr = (index: number) => {
  AttrFormData.attrValueList.splice(index, 1);
};

// 控制保存按钮禁用
const isbtnDisabled = computed(() => {
  return (
    !AttrFormData.attrValueList.some((attrValue) => attrValue.valueName) ||
    !AttrFormData.attrName
  );
});

const cartgroyStroe = useCartgroyStroe();
// 点击保存发送修改或增加请求
const addOrUpdateAttrList = async () => {
  await getAddAttrListApi({
    ...AttrFormData,
    categoryId: cartgroyStroe.cartgroy3Id,
    categoryLevel: 3,
    id: attrStore.id,
  });
  // cartgroyStroe.attrValue = "";
  // cartgroyStroe.attrAgree = [];
  emit("gobackAttrList", true);
  ElMessage.success(`${attrStore.id ? "修改" : "添加"}属性成功`);
};
</script>

<style scoped>
.mt-20 {
  margin-bottom: 20px;
}
</style>
